home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / security / xinetd / xinetd.2.0.6 / time.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-22  |  3.6 KB  |  182 lines

  1. /*
  2.  * (c) Copyright 1992 by Panagiotis Tsirigotis
  3.  * All rights reserved.  The file named COPYRIGHT specifies the terms 
  4.  * and conditions for redistribution.
  5.  */
  6.  
  7. static char RCSid[] = "$Id: time.c,v 5.2 1992/11/10 08:18:25 panos Exp $" ;
  8.  
  9. #include <syslog.h>
  10. #include <ctype.h>
  11. #include <time.h>
  12.  
  13. #include "fsma.h"
  14. #include "pset.h"
  15.  
  16. #include "defs.h"
  17.  
  18. time_t time() ;
  19.  
  20. void parsemsg() ;
  21. void out_of_memory() ;
  22.  
  23.  
  24. #define IN_RANGE( val, low, high )     ( (low) <= (val) && (val) <= (high) )
  25.  
  26. struct time_interval
  27. {
  28.     short min_start ;
  29.     short min_end ;
  30. } ;
  31.  
  32. #define TIP( p )        ( (struct time_interval *) (p) )
  33.  
  34. static fsma_h interval_allocator ;
  35.  
  36.  
  37. status_e interval_init()
  38. {
  39.     int flags = FSM_RETURN_ERROR ;
  40.  
  41. #ifdef DEBUG
  42.     flags |= FSM_ZERO_FREE ;
  43. #endif
  44.     interval_allocator = fsm_create( sizeof( struct time_interval ), 0, flags ) ;
  45.     return( ( interval_allocator == NULL ) ? FAILED : OK ) ;
  46. }
  47.  
  48.  
  49. /*
  50.  * Returns TRUE if the current time is within at leaset one of the intervals
  51.  */
  52. bool_int time_in_range( intervals )
  53.     pset_h intervals ;
  54. {
  55.     time_t current_time ;
  56.     register unsigned u ;
  57.     register short min_current ;
  58.     struct tm *tmp ;
  59.  
  60.     (void) time( ¤t_time ) ;
  61.     tmp = localtime( ¤t_time ) ;
  62.     min_current = tmp->tm_hour * 60 + tmp->tm_min ;
  63.  
  64.     for ( u = 0 ; u < pset_count( intervals ) ; u++ )
  65.     {
  66.         register struct time_interval *tip ;
  67.         
  68.         tip = TIP( pset_pointer( intervals, u ) ) ;
  69.         if ( IN_RANGE( min_current, tip->min_start, tip->min_end ) )
  70.             return( TRUE ) ;
  71.     }
  72.     return( FALSE ) ;
  73. }
  74.  
  75.  
  76. PRIVATE char *get_num( nump, min_val, max_val, s, stop_char )
  77.    register int *nump ;
  78.    int min_val, max_val ;
  79.    char *s ;
  80.    char stop_char ;
  81. {
  82.    register char *p = s ;
  83.    char *func = "get_num" ;
  84.  
  85.    for ( *nump = 0 ; isdigit( *p ) ; p++ )
  86.    {
  87.       *nump *= 10 ;
  88.       *nump += *p - '0' ;
  89.    }
  90.  
  91.    if ( *p != stop_char )
  92.    {
  93.       parsemsg( LOG_ERR, func, "incorrect time interval" ) ;
  94.       return( NULL );
  95.    }
  96.  
  97.    if ( ! IN_RANGE( *nump, min_val, max_val ) )
  98.    {
  99.       parsemsg( LOG_ERR, func, "invalid time interval" ) ;
  100.       return( NULL ) ;
  101.    }
  102.    return( p+1 ) ;
  103. }
  104.  
  105.  
  106.  
  107. /*
  108.  * Each interval should have the form:
  109.  *    hour:min-hour:min
  110.  * Example: 2:30-4:15
  111.  */
  112. status_e add_interval( iset, interval_str )
  113.     pset_h iset ;
  114.     char *interval_str ;
  115. {
  116.     struct time_interval *tip ;
  117.     int hours, minutes ;
  118.     register char *p ;
  119.     int min_start, min_end ;
  120.     char *func = "add_interval" ;
  121.  
  122.     if ( ( p = get_num( &hours, 0, 23, interval_str, ':' ) ) == NULL )
  123.         return( OK ) ;
  124.     if ( ( p = get_num( &minutes, 0, 59, p, '-' ) ) == NULL )
  125.         return( OK ) ;
  126.     min_start = hours * 60 + minutes ;
  127.  
  128.     if ( ( p = get_num( &hours, 0, 23, p, ':' ) ) == NULL )
  129.         return( OK ) ;
  130.     if ( ( p = get_num( &minutes, 0, 59, p, NUL ) ) == NULL )
  131.         return( OK ) ;
  132.     min_end = hours * 60 + minutes ;
  133.     if ( min_start >= min_end )
  134.     {
  135.         parsemsg( LOG_ERR, func, "invalid time interval: %s", interval_str ) ;
  136.         return( OK ) ;
  137.     }
  138.  
  139.     tip = TIP( fsm_alloc( interval_allocator ) ) ;
  140.     if ( tip == NULL )
  141.     {
  142.         out_of_memory( func ) ;
  143.         return( FAILED ) ;
  144.     }
  145.     tip->min_start = min_start ;
  146.     tip->min_end = min_end ;
  147.     if ( pset_add( iset, tip ) == NULL )
  148.     {
  149.         fsm_free( interval_allocator, (char *) tip ) ;
  150.         out_of_memory( func ) ;
  151.         return( FAILED ) ;
  152.     }
  153.     return( OK ) ;
  154. }
  155.  
  156.  
  157. void dump_intervals( iset, fd )
  158.     pset_h iset ;
  159.     int fd ;
  160. {
  161.     register unsigned u ;
  162.  
  163.     for ( u = 0 ; u < pset_count( iset ) ; u++ )
  164.     {
  165.         register struct time_interval *tip = TIP( pset_pointer( iset, u ) ) ;
  166.  
  167.         Sprint( fd, " %02d:%02d-%02d:%02d",
  168.             tip->min_start / 60, tip->min_start % 60,
  169.             tip->min_end / 60, tip->min_end % 60 ) ;
  170.     }
  171. }
  172.  
  173.  
  174. void free_intervals( iset )
  175.     pset_h iset ;
  176. {
  177.     void free_pset() ;
  178.  
  179.     free_pset( iset, interval_allocator ) ;
  180. }
  181.  
  182.